<?php
/**
 * 监理例会纪要
 * @author YangL
 **/
namespace Admin\Controller;
use Common\Controller\WahahabaseController;
class SupervisionMeetingController extends WahahabaseController{
	protected $labourplan_model;
	public function _initialize() {
		parent::_initialize();
		$this->deal_model = D("Common/SupervisionMeeting");
		$this->att_model = D("Common/Attachment");
		$this->labourplan_model = D("Common/LabourPlan");
		$this->check = array('issue');
	}
	//主页方法
	public function indexPage($where){
        $eq = array(
            "eq" => "=",
            "neq" => "!=",
            "gt" => ">",
            "egt" => ">=",
            "lt" => "<",
            "elt" => "<=",
            "like" => "LIKE"
        ); 
        // 当前登录人
        $adminId = session('ADMIN_ID');
        
        $condition = "(
            (d.is_publish = 0 AND (d.add_user = $adminId OR ((SELECT department_id FROM hzzy_users WHERE id=$adminId) = d.add_department) AND ((SELECT role_id FROM hzzy_users WHERE id=$adminId) IN('1','2','3'))))
            OR
            (d.is_publish = 1)
        )";
        foreach($where as $key => $v){
            if(count($v) > 1){
                $d = $eq[$v[0]] == NULL ? '=' : $eq[$v[0]];
                if($d != 'LIKE'){
                    $condition .= " AND $key $d '".$v[1]."'";
                } else {
                    $condition .= " AND $key $d '%".$v[1]."%'";
                }
            } else {
                $condition .= " AND $key = '$v'";
            }
        }

        $count=$this->deal_model->alias("d")->where($condition)->count();
        $page = $this->page($count, 20);
        
        $results = $this->deal_model->alias("d")
            ->field('d.*,u.real_name')
            ->join(array(
                ' LEFT JOIN '.C('DB_PREFIX').'users u ON d.add_user = u.id',
            ))
            ->where($condition)
            ->order("d.add_time DESC")
            ->limit($page->firstRow, $page->listRows)
            ->select();
		//exit($this->deal_model->getLastSql());	
        $this->assign("page", $page->show('Admin'));
        $this->assign("results",$results);
        
        $this->display();
    }
	public function index(){
		/**搜索条件**/
		$where = array("d.is_deleted"=>array('neq', 1));
		$issue = I('request.$issue');
		$this_week_problem = trim(I('request.this_week_problem'));
		if($issue){
			$where['issue'] = array('eq',"$issue");
		}
		if($this_week_problem){
			$where['this_week_problem'] = array('like',"%$this_week_problem%");
		}
		$this->getDepartLevel();
		
		$this->indexPage($where);
	}

	public function add(){
		$this->display();
	}

	//新增过往会议纪要
	public function adds(){
		$this->display();
	}
	public function add_post(){
		 if(IS_POST){
			$this->dataCheck();
            $data = array_merge($_POST, $this->saveData());
            if ($this->deal_model->create()!==false) {
                $result = $this->deal_model->add($data);
                if ($result!==false) {
                    $this->saveFile($result, 2);
					$this->saveLabourPlan(1, $result);	
                    $this->success("添加成功！", U("SupervisionMeeting/index"));
                } else {
                    $this->error("添加失败！");
                }
            } else {
                $this->error($this->deal_model->getError());
            }
        }
	}
	
	public function view(){
		$this->getDepartLevel();
		$this->getinfo();
	}
	public function views(){
		$this->getDepartLevel();
		$this->getinfo();
	}
	
	public function edit(){
		$this->getinfo();
	}
	public function edits(){
		$this->getinfo();
	}
	
	public function edit_post(){
		if (IS_POST) {
			$this->dataCheck();
			$data = array_merge($_POST, $this->saveData(2));
			if ($this->deal_model->create()!==false) {
				$result=$this->deal_model->save($data);
				if ($result !== false) {
					// 保存文件
					$this->saveFile(I("post.id"), 2, 2);
					// 删除投入
					$this->deleteLabourPlan(1, I("post.id"));
					// 投入信息
					$this->saveLabourPlan(1, I("post.id"));
					$this->success("保存成功！", U("SupervisionMeeting/index"));
				} else {
					$this->error("保存失败！");
				}
			} else {
				$this->error($this->deal_model->getError());
			}
		}
	}
	
	public function getinfo(){
		$id = I('get.id',0,'intval');

		$metting = $this->deal_model->where(array("id"=>$id))->find();
		$this->assign("metting", $metting);
		

		$tt=str_replace("-","",$metting['meeting_time']);
		$this->assign("tt", $tt);
		
		$results = $this->att_model->where(array("table_id"=>$id, "belong_to"=>2))->select();
		$this->assign("results", $results);
		
		$labels = $this->labourplan_model->where(array("table_id"=>$id, "belong_to"=>1, "plan_type" => 1))->select();
		$this->assign("labels1", $labels);
		
		$labels = $this->labourplan_model->where(array("table_id"=>$id, "belong_to"=>1, "plan_type" => 2))->select();
		$this->assign("labels2", $labels);
		
		$labels = $this->labourplan_model->where(array("table_id"=>$id, "belong_to"=>1, "plan_type" => 3))->select();
		$this->assign("labels3", $labels);
		
		$this->display();
	}
}